﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using My.DataAccess.Core;
using NCLCustomerManagement.DataTransferObjects.Product;

namespace NCLCustomerManagement.DataAccess.Products
{
    public class ProductRepository : BaseRepository<ProductDTO, Product>, IProductRepository
    {
        public IEnumerable<ProductDTO> GetAllProducts()
        {
            return UnitOfWork.Table<Product>()
                .Select(e => ConvertProduct(e))
                .AsEnumerable();
        }

        public ProductDTO ConvertProduct(Product entity)
        {
            return new ProductDTO()
            {
                Id = entity.Id,
                Available = entity.Available,
                CurrentSaleUnitPrice = entity.CurrentSaleUnitPrice,
                Description = entity.Description,
                ImageUrl = entity.ImageUrl,
                Madein = entity.Madein,
                PrimaryUnit = entity.PrimaryUnit,
                ProductCategoryId = entity.ProductCategoryId,
                ProductName = entity.ProductName,
                QuantityPerUnit = entity.QuantityPerUnit,
                SecondaryUnit = entity.SecondaryUnit,
                UsedPerDay = entity.UsedPerDay,
                ProductCategoryName = entity.ProductCategory.ProductCategoryName

            };
        }

        public Product ConvertProduct(ProductDTO dto)
        {
            return new Product()
            {
                Id = dto.Id,
                Available = dto.Available,
                CurrentSaleUnitPrice = dto.CurrentSaleUnitPrice,
                Description = dto.Description,
                ImageUrl = dto.ImageUrl,
                Madein = dto.Madein,
                PrimaryUnit = dto.PrimaryUnit,
                ProductCategoryId = dto.ProductCategoryId,
                ProductName = dto.ProductName,
                QuantityPerUnit = dto.QuantityPerUnit,
                SecondaryUnit = dto.SecondaryUnit,
                UsedPerDay = dto.UsedPerDay

            };
        }


        public void AddProduct(ProductDTO dto)
        {
            UnitOfWork.InsertOnSubmit<Product>(ConvertProduct(dto));
            UnitOfWork.SubmitChanges();
        }

        public void UpdateProduct(ProductDTO dto)
        {
            var entity = GetEntity(dto.Id);
            if (entity != null)
            {
                if (dto.ImageUrl != null)
                    entity.ImageUrl = dto.ImageUrl;
                entity.Madein = dto.Madein;
                entity.PrimaryUnit = dto.PrimaryUnit;
                entity.Available = dto.Available;
                entity.CurrentSaleUnitPrice = dto.CurrentSaleUnitPrice;
                entity.Description = dto.Description;
                entity.ProductCategoryId = dto.ProductCategoryId;
                entity.ProductName = dto.ProductName;
                entity.QuantityPerUnit = dto.QuantityPerUnit;
                entity.SecondaryUnit = dto.SecondaryUnit;
                entity.UsedPerDay = dto.UsedPerDay;
            }
            UnitOfWork.SubmitChanges();
        }

        public void DeleteProduct(int productId)
        {
            var entity = GetEntity(productId);
            if (entity != null)
            {
                UnitOfWork.DeleteOnSubmit<Product>(entity);
                UnitOfWork.SubmitChanges();
            }
        }

        private Product GetEntity(int id)
        {
            return UnitOfWork.Table<Product>().SingleOrDefault(e => e.Id == id);
        }


        public int GetProductCountOf(int categoryId)
        {
            var item = UnitOfWork.Table<ProductCategory>().SingleOrDefault(e => e.Id == categoryId);
            if (item != null)
            {
                return item.Products.Count;
            }
            return 0;
        }
    }
}
